package org.meetunexpectedly.service.login.impl;

import org.meetunexpectedly.exception.BaseException;
import org.meetunexpectedly.service.login.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description: 邮箱服务实现类
 * @Author: pupil
 * @Date: 2024/08/05 下午 8:23
 */
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private SimpleMailMessage simpleMailMessage;

    @Autowired
    private JavaMailSender javaMailSender;

    // 从配置文件中读取邮箱发送者信息
    @Value("${spring.mail.username}")
    private String fromName;

    @Override
    @Async
    public void send(String email, String context) {
        try {
            // 设置右键标题
            simpleMailMessage.setSubject("邂逅");
            simpleMailMessage.setFrom("邂逅服务中心<"+fromName+">");
            simpleMailMessage.setTo(email);
            simpleMailMessage.setText(context);
            javaMailSender.send(simpleMailMessage);
        } catch (MailException e) {
            throw new BaseException("邮箱验证码发送失败，请重新尝试!");
        }
    }
}
